/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */

var search = function (nums, target) {
    let left = 0
    let right = nums.length - 1
    let mid = Math.floor(nums.length / 2)
    while (left <= right) {
        // 如果目标值大于中间值 left = mid + 1
        if (target > nums[mid]) {
            left = mid + 1
        }
        // 如果目标值小于中间值 right = mid - 1
        else if (target < nums[mid]) {
            right = mid - 1
        }
        // 如果正好等于 则直接返回
        else {
            return mid
        }
        mid = Math.floor(right - (right - left) / 2)
    }
    return -1
};